/*
 * @Description: 动态表单提交记录数据模型
 */
import { ApiProperty } from '@nestjs/swagger';
import { modelOptions, prop, Ref } from '@typegoose/typegoose';
import { IsNotEmpty, IsOptional, IsObject } from 'class-validator';
import { FormTemplate } from './form-template.model';

// 表单提交数据
export class SubmissionData {
  @ApiProperty({ description: '字段名' })
  fieldName: string;

  @ApiProperty({ description: '字段值' })
  fieldValue: any;

  @ApiProperty({ description: '字段标签' })
  fieldLabel: string;

  @ApiProperty({ description: '字段类型' })
  fieldType: string;
}

@modelOptions({
  schemaOptions: {
    timestamps: true,
  },
})
export class FormSubmission {
  @ApiProperty({ description: '关联的表单模板ID' })
  @IsNotEmpty({ message: '表单模板ID不能为空' })
  @prop({ ref: () => FormTemplate, required: true })
  formTemplateId: Ref<FormTemplate>;

  @ApiProperty({ description: '表单名称（冗余存储，便于查询）' })
  @IsOptional()
  @prop()
  formName?: string;

  @ApiProperty({ description: '提交的表单数据' })
  @IsObject({ message: '表单数据必须为对象' })
  @prop({ type: Object, required: true })
  formData: Record<string, any>;

  @ApiProperty({ description: '结构化的提交数据' })
  @prop({ type: [SubmissionData], default: [] })
  submissionData: SubmissionData[];

  @ApiProperty({ description: '提交者IP地址' })
  @IsOptional()
  @prop()
  submitterIp?: string;

  @ApiProperty({ description: '提交者用户代理' })
  @IsOptional()
  @prop()
  userAgent?: string;

  @ApiProperty({ description: '提交者ID（如果已登录）' })
  @IsOptional()
  @prop()
  submitterId?: string;

  @ApiProperty({ description: '提交状态' })
  @IsOptional()
  @prop({ default: 'submitted' })
  status?: string; // submitted, processed, archived

  @ApiProperty({ description: '处理备注' })
  @IsOptional()
  @prop()
  processNote?: string;

  @ApiProperty({ description: '处理者ID' })
  @IsOptional()
  @prop()
  processedBy?: string;

  @ApiProperty({ description: '处理时间' })
  @IsOptional()
  @prop()
  processedAt?: Date;

  @ApiProperty({ description: '提交来源' })
  @IsOptional()
  @prop()
  source?: string; // web, mobile, api

  _id: string;
  createdAt: Date;
  updatedAt: Date;
}